﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EShop.Client
{
    public partial class frmMerchandiseOverTimeCheck : frmBase
    {
        public frmMerchandiseOverTimeCheck()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 总记录数量
        /// </summary>
        int iTotalCount = 0;
        /// <summary>
        /// 每页最大数量
        /// </summary>
        int iPageSize = 0;
        /// <summary>
        /// 总页数
        /// </summary>
        int iPageCount = 0;
        /// <summary>
        /// 当前页码
        /// </summary>
        int iCurrentPage = 0;
        /// <summary>
        /// 所有数据行
        /// </summary>
        /// 
        DataTable dt = new DataTable();

        int iAlertEarlierDay = 10;

        private void frmMerchandiseOverTimeCheck_Load(object sender, EventArgs e)
        {
            btnMoveFirstItem.Enabled = true;
            btnMoveLastItem.Enabled = true;
            btnMoveNextItem.Enabled = true;
            btnMovePreviousItem.Enabled = true;
            txtPositionItem.Enabled = true;
            lblCountItem.Enabled = true;
            iPageSize = oShopConfigInfo.PageSize;
            Init();
        }

        private void Init()
        {
            dt = new BLL.MerchandiseAlertBAL().MerchandiseOverTimeAlert(iAlertEarlierDay);
            iTotalCount = dt.Rows.Count;
            iPageCount = iTotalCount / iPageSize;
            if (iTotalCount % iPageSize != 0) iPageCount += 1;
            lblCountItem.Text = "/" + iPageCount.ToString();
            BindData();
        }
        private void BindData()
        {
            txtPositionItem.Text = (iCurrentPage + 1).ToString();
            int iPosStart, iPosEnd;
            if (iCurrentPage >= iPageCount - 1)
            {
                iPosStart = iCurrentPage * iPageSize;
                iPosEnd = iTotalCount;
            }
            else 
            {
                iPosStart = iCurrentPage * iPageSize;
                iPosEnd = iPosStart + iPageSize;
            }
            dgvCheck.Rows.Clear();
            int index = -1;
            for (int i = iPosStart; i < iPosEnd; i++)
            {
                index = dgvCheck.Rows.Add();
                dgvCheck.Rows[index].Cells["StockInOrder"].Value = dt.Rows[i]["StockInOrder"];
                dgvCheck.Rows[index].Cells["Barcode"].Value = dt.Rows[i]["Barcode"];
                dgvCheck.Rows[index].Cells["MerchandiseName"].Value = dt.Rows[i]["MerchandiseName"];
                dgvCheck.Rows[index].Cells["StockInNum"].Value = dt.Rows[i]["StockInNum"];
                dgvCheck.Rows[index].Cells["IsStatu"].Value = dt.Rows[i]["IsStatu"].ToString() == "0" ? "待上架" : "已上架";
                dgvCheck.Rows[index].Cells["OverTime"].Value = ((DateTime)dt.Rows[i]["OverTime"]).ToString("yyyy-MM-dd");
                dgvCheck.Rows[index].Cells["StockInTime"].Value = ((DateTime)dt.Rows[i]["StockInTime"]).ToString("yyyy-MM-dd");
            }
        }

        private void btnMoveFirstItem_Click(object sender, EventArgs e)
        {
            if (iCurrentPage == 0) return;
            iCurrentPage = 0;
            BindData();
        }

        private void btnMovePreviousItem_Click(object sender, EventArgs e)
        {
            if (iCurrentPage == 0) return;
            iCurrentPage = iCurrentPage - 1;
            BindData();
        }

        private void btnMoveNextItem_Click(object sender, EventArgs e)
        {
            if (iCurrentPage >= iPageCount - 1) return;
            iCurrentPage = iCurrentPage + 1;
            BindData();
        }

        private void btnMoveLastItem_Click(object sender, EventArgs e)
        {
            if (iCurrentPage == iPageCount - 1) return;
            iCurrentPage = iPageCount - 1;
            BindData();
        }

        private void txtPositionItem_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                int page = iCurrentPage + 1;
                try
                {
                    int gotoPage = Convert.ToInt32(txtPositionItem.Text);
                    if (gotoPage == page) return;
                    if (gotoPage < 1 || gotoPage > iPageCount) { txtPositionItem.Text = page.ToString(); return; }
                    iCurrentPage = gotoPage - 1;
                    BindData();
                }
                catch
                {
                    txtPositionItem.Text = page.ToString();
                }
                finally { this.txtPositionItem.SelectAll(); }
            }
        }

        private void txtPositionItem_Click(object sender, EventArgs e)
        {
            this.txtPositionItem.SelectAll();
        }

    }
}
